package common.dao.couchdb;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import common.helper.json.JsonToJavaObject;
import common.helper.toolkit.HttpToolkit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Common on 11/26/16.
 */
public class CouchDBHttpClient {
    static final Logger logger = LoggerFactory.getLogger(CouchDBHttpClient.class);

    private String host = "";
    private int port = 8080;
    private String base_url = "";

    public CouchDBHttpClient(){
        //this.base_url = "http://" + this.host + ":" + this.port + "/";
    }

    public void setHostPort(String host, int port){
        this.host = host;
        this.port = port;
        this.base_url = "http://" + this.host + ":" + this.port + "/";
    }



    public ObjectNode get_doc_by_id( String db, String id ){
        String url = this.base_url + "/" + db + "/" + id;
        logger.info("[get_doc_by_id] url = {}", url);
        String ret_string = HttpToolkit.REST_GET(url);
        logger.info("[ret_string] = {}", ret_string);
        JsonNode ret_node = JsonToJavaObject.loadJsonNode( ret_string );
        ObjectNode obj_node = (ObjectNode)ret_node;
        return obj_node;
    }

    public ObjectNode get_all_docs(String db, String start_id, int limit){
        /*
        http://docs.couchdb.org/en/2.0.0/api/database/bulk-api.html
         */
        String url = this.base_url + "/" + db + "/" + "_all_docs" + "?startkey_docid=" + start_id + "&limit=" + limit
                + "&descending=false" + "&update_seq=true";
        logger.info("[get_all_docs] url = {}", url);
        //String ret_str = HttpToolkit.rest_get( url );
        String ret_str = HttpToolkit.REST_GET(url);
        logger.info("[ret_str] = {}", ret_str);
        JsonNode ret_node = JsonToJavaObject.loadJsonNode( ret_str );
        ObjectNode obj_node = (ObjectNode)ret_node;
        return obj_node;
    }

    public List<String> get_doc_ids(String db, String start_id, int limit){
        ObjectNode ret_node = get_all_docs(db,start_id,limit);
        ArrayNode arr_node = (ArrayNode)ret_node.get("rows");
        List<String> ids = new ArrayList<>();
        for(JsonNode node: arr_node){
            String id = node.get("id").asText();
            ids.add(id);
        }
        return ids;
    }


    /*
    *****************************************
    * MAIN
     */
    public static void main(String[] args){
        CouchDBHttpClient client = new CouchDBHttpClient();
        client.setHostPort("162.105.180.15", 15984);
        //ObjectNode ret_node = client.get_doc_by_id("kgmeta", "concept-20409");
        //String _id = ret_node.get("_id").asText();
        //String _rev = ret_node.get("_rev").asText();
        //String ret_string = JsonToJavaObject.jsonNodeToString( ret_node );
        //logger.info("[ret], _id =  {}, _rev = {}", _id, _rev);

        //ObjectNode ret_node = client.get_all_docs("kgmeta", "concept-204092", 10);
        //String ret_str = JsonToJavaObject.jsonNodeToString( ret_node );
        //logger.info("[ret_str] = {}", ret_str);
        //ret_node = client.get_all_docs("kgmeta", "concept-204092", 5);
        //ret_str = JsonToJavaObject.jsonNodeToString( ret_node );
        //logger.info("[ret_str] = {}", ret_str);

        String db_name = "kgmeta";
        List<String> ids = client.get_doc_ids(db_name, "concept-204092", 5);
        for(String id:ids){
            logger.info("[id=] {}", id);
            ObjectNode doc = client.get_doc_by_id(db_name, id);
            String rev = doc.get("_rev").asText();
            logger.info("[rev=] {}", rev);
            logger.info("\n");
        }

    }

}
